﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace CountDown
{
    public partial class SetForm : Form
    {
        private TimeSpan ts = new TimeSpan(0, 0, 0);
        public SetForm()
        {
            InitializeComponent();

            //窗体
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.None;//不显示标题栏和任务栏
            this.TopMost = true;
            this.Text = "倒计时设置";
            RefreshDisplay();

            //透明度
            OpacityBarTrackBar.Minimum = 0;
            OpacityBarTrackBar.Maximum = 9;
            OpacityBarTrackBar.ValueChanged += TrackBar1_ValueChanged;
        }


        //基础时间
        private void Datum_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            String butName = button.Name;
            int minute = Convert.ToInt16(butName.Replace("But", ""));
            ts = new TimeSpan(0, minute, 0);
            RefreshDisplay();
        }

        //减操作
        private void Cut_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            String butName = button.Name;
            int second = Convert.ToInt16(butName.Replace("ButCut", ""));
            if (ts.TotalSeconds <= 0)
            {
                MessageBox.Show("不能设置负时间!");
                return;
            }
            ts = ts.Subtract(new TimeSpan(0, 0, second));
            RefreshDisplay();
        }


        //加操作
        private void Plus_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            String butName = button.Name;
            int second = Convert.ToInt16(butName.Replace("ButPlus", ""));
            if (ts.TotalSeconds >= 3600)
            {
                MessageBox.Show("不能设置超过60分钟!");
                return;
            }
            ts = ts.Add(new TimeSpan(0, 0, second));
            RefreshDisplay();
        }

        private void RefreshDisplay()
        {
            label1.Text = ts.Hours.ToString().PadLeft(2, '0') + ":" + ts.Minutes.ToString().PadLeft(2, '0') + ":" + ts.Seconds.ToString().PadLeft(2, '0');
        }

        private void ButExit_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void ButUse_Click(object sender, EventArgs e)
        {
            if (ts.TotalSeconds <= 0)
            {
                MessageBox.Show("请设置好时间!");
                return;
            }
            Main.SetInitialTime((int)ts.TotalSeconds);
            this.Hide();
        }

        private void SetLocation_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            Main.SetLocation(button.Name.Split('_')[0]);
        }

        private void TrackBar1_ValueChanged(object sender, EventArgs e)
        {
            double value = 0.1 + OpacityBarTrackBar.Value * 0.1;
            Main.SetOpacity(value);
        }

        private void Quick_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            MatchCollection matches = Regex.Matches(button.Text, @"[-+]?\d+\.?\d*");
            String minute = matches[0].Value;
            Main.SetInitialTime(int.Parse(minute) * 60);
            this.Hide();
        }
    }
}
